package com.maxima.attendance.daos;

import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.maxima.attendance.pojos.Employee;
import com.maxima.attendance.pojos.User;
import com.maxima.attendance.utils.HibernateUtils;

public class UserDAO {
	Map<String, Object> map;

	public User add(User u) {
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		session.save(u);
		session.getTransaction().commit();
		session.close();
		return u;
	}

	public void changePassword(Integer id, String password) {
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		User user = null;
		try {
			user = (User) session.createQuery("select u from User u where u.id=" + id).uniqueResult();
			user.setPassword(password);
		}
		catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		session.close();
	}

	@SuppressWarnings("unchecked")
	public List<User> getAllUsers() {
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		List<User> users = null;
		try {
			users = (List<User>) session.createQuery("select u from User u order by u.firstName").list();
		}
		catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		session.close();
		return users;
	}

	@SuppressWarnings("unchecked")
	public List<User> getAllActiveUsers() {
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		List<User> users = null;
		try {
			users = (List<User>) session.createQuery("select u from User u where u.active=1 order by u.firstName").list();
		}
		catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		session.close();
		return users;
	}

	@SuppressWarnings("unchecked")
	public List<String> getAllProjectNames() {
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		List<String> users = null;
		try {
			users = (List<String>) session.createQuery("select distinct u.project from User u order by u.project").list();
		}
		catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		session.close();
		return users;
	}
	
	@SuppressWarnings("unchecked")
	public List<User> getAllUsersOfSameProject(Integer projectId) {
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		List<User> users = null;
		try {
			users = (List<User>) session.createQuery("select u from User u where u.project.id =" + projectId).list();
		}
		catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		session.close();
		return users;
	}
	
	public boolean checkIfAlreadyPresent(Integer employeeId) {
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		Criteria criteria = session.createCriteria(User.class);
		criteria.add(Restrictions.eq("employeeId", employeeId));
		Integer x = criteria.list().size();
		session.close();
		return x != 0;
	}

	public User getUser(Integer id) {
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		User user = null;
		try {
			user = (User) session.createQuery("select u from User u where u.id=" + id).uniqueResult();
		}
		catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		session.close();
		return user;
	}

	public Employee getEmployee(Integer id) {
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		User user = null;
		Employee emp = null;
		try {
			user = (User) session.createQuery("select u from User u where u.id=" + id).uniqueResult();
			emp = new Employee(user, new TimeLogDAO().getLastTimeLog(id));
		}
		catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		session.close();
		return emp;
	}

	public void update(User existingUser) {
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		User user = null;
		try {
			user = (User) session.createQuery("select u from User u where u.id=" + existingUser.getId()).uniqueResult();
			user.setFirstName(existingUser.getFirstName());
			user.setLastName(existingUser.getLastName());
			user.setEmployeeId(existingUser.getEmployeeId());
			user.setPassword(existingUser.getPassword());
			user.setProject(existingUser.getProject());
			user.setActive(existingUser.getActive());
		}
		catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		session.close();
	}
}
